package org.ricks.common.db;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author chenwei
 * @Title:
 * @Package
 * @Description:
 *  =================================  犬酱组方式 start  ================================
 *
 *   private <T> T getResult(ResultSet var1, T var2) throws Exception {
 *         for(int var3 = 1; var3 <= var1.getMetaData().getColumnCount(); ++var3) {
 *             String var4 = var1.getMetaData().getColumnLabel(var3);
 *             switch(var1.getMetaData().getColumnType(var3)) {
 *                 case -7:
 *                 case 16:
 *                     this.setProperty(var2, var4, var1.getBoolean(var4));
 *                     break;
 *                 case -6:
 *                     this.setProperty(var2, var4, var1.getByte(var4));
 *                     break;
 *                 case -5:
 *                     this.setProperty(var2, var4, var1.getLong(var4));
 *                     break;
 *                 case -4:
 *                 case -3:
 *                 case -2:
 *                 case 2003:
 *                 case 2004:
 *                     Blob var11 = var1.getBlob(var3);
 *                     if (var11 != null) {
 *                         InputStream var12 = var11.getBinaryStream();
 *
 *                         try {
 *                             ObjectInputStream var8 = new ObjectInputStream(var12);
 *                             this.setProperty(var2, var4, var8.readObject());
 *                             var8.close();
 *                         } catch (ClassNotFoundException | IOException var10) {
 *                             throw new SQLException(var10);
 *                         }
 *                     }
 *                     break;
 *                 case 1:
 *                 case 12:
 *                     this.setProperty(var2, var4, var1.getString(var4));
 *                     break;
 *                 case 3:
 *                     this.setProperty(var2, var4, var1.getBigDecimal(var4));
 *                     break;
 *                 case 4:
 *                     this.setProperty(var2, var4, var1.getInt(var4));
 *                     break;
 *                 case 5:
 *                     this.setProperty(var2, var4, var1.getShort(var4));
 *                     break;
 *                 case 6:
 *                     this.setProperty(var2, var4, var1.getFloat(var4));
 *                     break;
 *                 case 8:
 *                     this.setProperty(var2, var4, var1.getDouble(var4));
 *                     break;
 *                 case 91:
 *                     this.setProperty(var2, var4, var1.getDate(var4));
 *                     break;
 *                 case 93:
 *                     this.setProperty(var2, var4, var1.getTimestamp(var4));
 *                     break;
 *                 case 2005:
 *                     Clob var5 = var1.getClob(var3);
 *                     if (var5 != null) {
 *                         char[] var6 = new char[(int)var5.length()];
 *                         Reader var7 = var1.getClob(var3).getCharacterStream();
 *
 *                         try {
 *                             var7.read(var6);
 *                             this.setProperty(var2, var4, new String(var6));
 *                         } catch (IOException var9) {
 *                             throw new SQLException();
 *                         }
 *                     }
 *                     break;
 *                 default:
 *                     this.setProperty(var2, var4, var1.getObject(var4));
 *             }
 *         }
 *
 *         return var2;
 *     }
 *
 *     private void setProperty(Object var1, String var2, Object var3) throws Exception {
 *         BeanWrapper var4 = new BeanWrapper(var1);
 *         var4.setPropertyValue(var2, var3);
 *     }
 *
 *     ===================================== 犬酱组方式 end ================================================
 *
 *
 *     =====================================  反射暴力set方式 start =========================================
 *
 *      public static <T> T getObj(ResultSet rs, Class<T> clazz) {
 *         // 获取JavaBean里面的所有属性
 *         Field[] field = clazz.getDeclaredFields();
 *         T obj = null;
 *         try {
 *             ResultSetMetaData rsmd = rs.getMetaData();
 *             // 获取记录集中的列数
 *             int counts = rsmd.getColumnCount();
 *             // 定义counts个String 变量
 *             String[] columnNames = new String[counts];
 *             // 给每个变量赋值(字段名称全部转换成大写)
 *             for (int i = 0; i < counts; i++) {
 *                 columnNames[i] = rsmd.getColumnLabel(i + 1).toUpperCase();
 *             }
 *             List columnNameList = Arrays.asList(columnNames);
 *             // System.out.println(columnNameList);
 *
 *             // 获取第一条记录
 *             if (rs.next()) {
 *                 // 创建Javabean对象
 *                 obj = clazz.getDeclaredConstructor().newInstance();
 *                 Class tempClazz = clazz;
 *                 while (tempClazz != null && tempClazz != Object.class) {
 *                     // 循环将查询结果写入对应的JavaBean属性中
 *                     for (Field f : field) {
 *                         // 获取该字段名称
 *                         String name = f.getName();
 *                         // 判断该字段是否在ResultSet的字段里，在的话才去进行赋值操作
 *                         if (columnNameList.contains(name.toUpperCase())) {
 *                             // 判断是否查询到对应的值
 *                             if (rs.getObject(name) != null) {
 *                                 // 跳过检查，这里访问的时私有属性
 *                                 f.setAccessible(true);
 *                                 // 将查询到的值付给对应的属性
 *                                 f.set(obj, rs.getObject(name));
 *                             }
 *                         }
 *                     }
 *                     tempClazz = tempClazz.getSuperclass();
 *                     field = tempClazz.getDeclaredFields();
 *                 }
 *             }
 *         } catch (SQLException e) {
 *             System.err.println("结果集操作失败");
 *             e.printStackTrace();
 *         } catch (Exception e) {
 *             System.err.println("数据读取失败");
 *             e.printStackTrace();
 *         }
 *         return obj;
 *     }
 *
 *     ===================================== 反射暴力set方式 end ================================
 *
 *
 *
 * @date 2020/12/2316:42
 */
public class BeanHandler<T> implements ResultSetHandler<T> {
    private final Class<? extends T> type;
    private final RowProcessor convert;

    public BeanHandler(Class<? extends T> type) {
        this(type, ArrayHandler.ROW_PROCESSOR);
    }

    public BeanHandler(Class<? extends T> type, RowProcessor convert) {
        this.type = type;
        this.convert = convert;
    }

    public T handle(ResultSet rs) throws SQLException {
        return rs.next() ? this.convert.toBean(rs, this.type) : null;
    }

}
